package ezXpns.GUI;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;

import javax.swing.JComponent;
import javax.swing.JProgressBar;
import javax.swing.plaf.basic.BasicProgressBarUI;
import java.awt.Font;

/**
 * @author A0086931X
 */

//@author A0086931X
public class ProgressBar extends JProgressBar {

	private static final long serialVersionUID = 1L;

	public ProgressBar() {
		setFont(new Font("Dialog", Font.BOLD, 24));
		setValue(0);
		setStringPainted(true);
		setUI(new GradientPalletProgressBarUI());
	}

	public void setValue(float totalIncome, float max) {
		int percentage = (int) (totalIncome * 100 / max);
		super.setValue(percentage);
		setString("" + String.format("%.2f", totalIncome) + " / " + String.format("%.2f", max));
	}
}

class GradientPalletProgressBarUI extends BasicProgressBarUI {
	private final int[] pallet;

	public GradientPalletProgressBarUI() {
		super();
		this.pallet = makeGradientPallet();
	}

	private static int[] makeGradientPallet() {
		BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = image.createGraphics();
		Point2D start = new Point2D.Float(0f, 0f);
		Point2D end = new Point2D.Float(99f, 0f);
		float[] dist = { 0.0f, 0.5f, 1.0f };
		Color[] colors = { Color.GREEN, Color.YELLOW, Color.RED };
		g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
		g2.fillRect(0, 0, 100, 1);
		g2.dispose();

		int width = image.getWidth(null);
		int[] pallet = new int[width];
		PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
		try {
			pg.grabPixels();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pallet;
	}

	@Override
	public void paintDeterminate(Graphics g, JComponent c) {
		if (!(g instanceof Graphics2D)) {
			return;
		}
		Insets b = progressBar.getInsets(); // area for border
		int barRectWidth = progressBar.getWidth() - (b.right + b.left);
		int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);
		if (barRectWidth <= 0 || barRectHeight <= 0) {
			return;
		}

		// amount of progress to draw
		int amountFull = getAmountFull(b, barRectWidth, barRectHeight);
		float x = amountFull / (float) barRectWidth;
		if (x >= 0.7) {
			g.setColor(new Color(255, 0, 0));
		} else if (x >= 0.5) {
			g.setColor(Color.YELLOW);
		} else
			g.setColor(Color.GREEN);

		g.setColor(getColorFromPallet(pallet, x)); // for gradient colour.

		g.fillRect(b.left, b.top, amountFull, barRectHeight);

		if (progressBar.isStringPainted()) {
			g.setColor(new Color(0, 0, 0));
			paintString(g, b.left, b.top, barRectWidth, barRectHeight, amountFull, b);
		}
	}

	private static Color getColorFromPallet(int[] pallet, float x) {
		if (x < 0.0 || x > 1.0) {
			throw new IllegalArgumentException("Parameter outside of expected range");
		}
		int i = (int) (pallet.length * x);
		int max = pallet.length - 1;
		int index = i < 0 ? 0 : i > max ? max : i;
		int pix = pallet[index] & 0x00ffffff | (0x64 << 24);
		return new Color(pix, true);
	}
}